package com.kexio.common.enums;

/**
 * 操作类型枚举
 * 用于权限控制和操作记录
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public enum OperateType {
    
    /**
     * 创建操作
     */
    CREATE("CREATE", "创建", "C"),
    
    /**
     * 读取操作
     */
    READ("READ", "读取", "R"),
    
    /**
     * 更新操作
     */
    UPDATE("UPDATE", "更新", "U"),
    
    /**
     * 删除操作
     */
    DELETE("DELETE", "删除", "D"),
    
    /**
     * 执行操作
     */
    EXECUTE("EXECUTE", "执行", "X"),
    
    /**
     * 管理操作
     */
    MANAGE("MANAGE", "管理", "M"),
    
    /**
     * 访问操作
     */
    ACCESS("ACCESS", "访问", "A"),
    
    /**
     * 配置操作
     */
    CONFIG("CONFIG", "配置", "CF"),
    
    /**
     * 审批操作
     */
    APPROVE("APPROVE", "审批", "AP"),
    
    /**
     * 监控操作
     */
    MONITOR("MONITOR", "监控", "MO");
    
    private final String code;
    private final String desc;
    private final String shortCode;
    
    OperateType(String code, String desc, String shortCode) {
        this.code = code;
        this.desc = desc;
        this.shortCode = shortCode;
    }
    
    public String getCode() {
        return code;
    }
    
    public String getDesc() {
        return desc;
    }
    
    public String getShortCode() {
        return shortCode;
    }
    
    /**
     * 根据code获取枚举
     */
    public static OperateType fromCode(String code) {
        if (code == null) {
            return null;
        }
        for (OperateType type : values()) {
            if (type.code.equalsIgnoreCase(code)) {
                return type;
            }
        }
        throw new IllegalArgumentException("未知的操作类型: " + code);
    }
    
    /**
     * 根据短代码获取枚举
     */
    public static OperateType fromShortCode(String shortCode) {
        if (shortCode == null) {
            return null;
        }
        for (OperateType type : values()) {
            if (type.shortCode.equalsIgnoreCase(shortCode)) {
                return type;
            }
        }
        throw new IllegalArgumentException("未知的操作类型短代码: " + shortCode);
    }
    
    /**
     * 是否为写操作
     */
    public boolean isWriteOperation() {
        return this == CREATE || this == UPDATE || this == DELETE || this == EXECUTE
                || this == MANAGE || this == CONFIG || this == APPROVE;
    }
    
    /**
     * 是否为读操作
     */
    public boolean isReadOperation() {
        return this == READ || this == ACCESS || this == MONITOR;
    }
    
    @Override
    public String toString() {
        return "OperateType{" +
                "code='" + code + '\'' +
                ", desc='" + desc + '\'' +
                ", shortCode='" + shortCode + '\'' +
                '}';
    }
}
